

package org.koyabr.webx.demo.danmaku.biz.impl;

import org.koyabr.webx.demo.danmaku.biz.DuplicatedUserException;
import org.koyabr.webx.demo.danmaku.biz.UserManager;
import org.koyabr.webx.demo.danmaku.dal.dao.UserDao;
import org.koyabr.webx.demo.danmaku.dal.dataobjs.User;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * 用户相关的操作实现
 */
public class UserManagerImpl implements UserManager {
    @Autowired
    private UserDao userDao;


    public User login(String username, String password) {
        return userDao.getAuthenticatedUser(username, password);
    }


    public void register(User user) throws DuplicatedUserException {
        User oldUser = userDao.getUserByName(user.getUsername());

        if (oldUser != null) {
            throw new DuplicatedUserException("duplicated user: " + user.getUsername());
        }

        userDao.insertUser(user);
    }

    public void update(User user) {
        userDao.updateUser(user);
    }

    public User getUser(String username) {
        return userDao.getUserByName(username);
    }
}
